﻿using Newtonsoft.Json;
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace FishScore
{
    public class DataFile
    {
        public static FishGameObject LoadDataFile(string file)
        {
            if (!File.Exists(file))
                return null;

            return JsonConvert.DeserializeObject<FishGameObject>(File.ReadAllText(file));
        }

        public static int SaveDataFile(FishGameObject game, string file)
        {
            if (game == null) return -1;

            try
            {
                File.WriteAllText(file, JsonConvert.SerializeObject(game, Formatting.Indented));
                return 0;
            }
            catch (Exception)
            {
                return -1;
                throw;
            }
        }

        /// <summary>
        /// 将表格数据导出到csv表格文件中
        /// </summary>
        /// <param name="dgv"></param>
        public static void ExportToExcel(DataGridView dgv)
        {
            ExportToExcel(dgv, "表格数据");
        }

        /// <summary>
        /// 将表格数据导出到csv表格文件中
        /// </summary>
        /// <param name="dgv"></param>
        public static void ExportToExcel(DataGridView dgv, string fileName)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "表格数据文件(*.csv)|*.csv";
            sfd.FileName = string.Format("{0}.csv", fileName);
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                fileName = sfd.FileName;
                ExportHelper.ExportDetails(GetDataTable(dgv), ExportHelper.ExportFormat.CSV, fileName, ExportHelper.ApplicationType.WindowsForm);
            }
        }

        /// <summary>
        /// 将表格数据导出到文本文件中
        /// </summary>
        /// <param name="dgv"></param>
        public static void ExportToText(DataGridView dgv)
        {
            ExportToText(dgv, "表格数据");
        }

        /// <summary>
        /// 将表格数据导出到文本文件中
        /// </summary>
        /// <param name="dgv"></param>
        public static void ExportToText(DataGridView dgv, string fileName)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "文本文件(*.txt)|*.txt";
            sfd.FileName = string.Format("{0}.txt", fileName);
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                fileName = sfd.FileName;
                ExportHelper.ExportDetails(GetDataTable(dgv), ExportHelper.ExportFormat.TXT, fileName, ExportHelper.ApplicationType.WindowsForm);
            }
        }

        /// <summary>
        /// 将dgv列表数据转换为datatable数据
        /// </summary>
        /// <param name="dgv">当前dgv列表对象</param>
        /// <returns>datatable对象</returns>
        public static DataTable GetDataTable(DataGridView dgv)
        {
            DataTable dt = new DataTable();

            // 列强制转换
            for (int count = 0; count < dgv.Columns.Count; count++)
            {
                System.Data.DataColumn dc = new System.Data.DataColumn(dgv.Columns[count].HeaderText.ToString());
                dt.Columns.Add(dc);
            }

            // 循环行
            for (int count = 0; count < dgv.Rows.Count; count++)
            {
                DataRow dr = dt.NewRow();
                for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
                {
                    dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

        //DataGridView导出到csv文件
        public static void TableSave(DataGridView dataGridView1)
        {
            string path = "";
            SaveFileDialog fdg = new SaveFileDialog();
            fdg.FileName = "分区成绩表.csv";

            try
            {
                if (fdg.ShowDialog() == DialogResult.OK)
                {
                    path = fdg.FileName;
                }
                else
                {
                    throw new Exception();
                }

                using (StreamWriter sw = new StreamWriter(path))
                {
                    sw.WriteLine(string.Join(",", dataGridView1.Columns.Cast<DataGridViewColumn>()
                                                     .Select(x => x.HeaderText)));

                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        sw.WriteLine(string.Join(",", row.Cells.Cast<DataGridViewCell>()
                                                    .Select(x => x.Value)));
                    }
                }
            }
            catch (Exception)
            {
            }
        }
    }
}